## Set values
# Hide welcome message & ensure we are reporting fish as shell
set fish_greeting
set VIRTUAL_ENV_DISABLE_PROMPT "1"
set -x SHELL /usr/bin/fish

## Environment setup
# Apply .profile: use this to put fish compatible .profile stuff in
if test -f ~/.fish_profile
  source ~/.fish_profile
end

if test -f ~/.config/fish/secret.fish
    source ~/.config/fish/secret.fish
end

# Add ~/.local/bin to PATH
if test -d ~/.local/bin
    if not contains -- ~/.local/bin $PATH
        set -p PATH ~/.local/bin
    end
end

## Tool configurations
# Use bat for man pages
set -xU MANPAGER "sh -c 'col -bx | bat -l man -p'"
set -xU MANROFFOPT "-c"

## Prompt configuration
## Starship prompt
if status --is-interactive
   source ("/usr/bin/starship" init fish --print-full-init | psub)
   export PATH="$HOME/.cargo/bin:$PATH"
end

## Pure theme configuration
if not functions -q fish_prompt
    fisher install pure-fish/pure
end
set -g pure_enable_git true
set -g pure_color_primary blue
set -g pure_color_success green
set -g pure_color_normal white
set -g pure_color_danger red
set -g pure_color_light white
set -g pure_color_dark black
# Git 状态显示优化
set -g pure_symbol_git_dirty "*"
set -g pure_symbol_git_unpulled_commits "⇣"
set -g pure_symbol_git_unpushed_commits "⇡"
set -g pure_symbol_git_stash "≡"

## Command helpers
## Advanced command-not-found hook
source /usr/share/doc/find-the-command/ftc.fish

## Functions
# Fish command history
function history
    builtin history --show-time='%F %T '
end

## Aliases
# Exit commands
alias q="exit"
alias :q="exit"

# File listing with eza
alias ls 'eza -l --color=always --group-directories-first --icons'  # long format
alias lt 'eza -aT --color=always --group-directories-first --icons' # tree listing

# Better alternatives
abbr cat 'bat --style header,snip,changes'

# Directory navigation
alias .. 'cd ..'
alias ... 'cd ../..'
alias .... 'cd ../../..'

# System utilities
alias fixpacman 'sudo rm /var/lib/pacman/db.lck'
alias grep 'ugrep --color=auto'
alias egrep 'ugrep -E --color=auto'
alias fgrep 'ugrep -F --color=auto'
alias ip 'ip -color'
alias upd '/usr/bin/garuda-update'
alias wget 'wget -c '

# Package management
alias mirror 'sudo reflector -f 30 -l 30 --number 10 --verbose --save /etc/pacman.d/mirrorlist'
alias please 'sudo'

## Application settings
alias vi="nvim"
set -gx EDITOR nvim
set -gx VISUAL nvim
set -gx OPENER xdg-open

## Key bindings
## Fish Vi 模式 Colemak 键位映射
function fish_user_key_bindings
    # 启用 Vi 模式
    fish_vi_key_bindings
    
    # 只设置 hnei 作为移动键位
    # 正常模式下的映射
    bind -M normal h backward-char    # 左移 (原功能：左移)
    bind -M normal n down-line        # 下移 (原功能：命令历史搜索)
    bind -M normal e up-line          # 上移 (原功能：上移一行)
    bind -M normal i forward-char     # 右移 (原功能：进入插入模式)
    bind -M normal u insert-mode      # 进入插入模式 (替代原来的 i 键)
    bind -M normal z history-search-backward  # 历史命令搜索 (原功能：无关键功能)
    bind -M normal q 'exit; commadline -f repaint'
end

# 替换 pacman -Syu 为 garuda-update
function pacman
    if test "$argv[1]" = "-Syu"
        garuda-update
    else
        command pacman $argv
    end
end

## Tool initialization
# 初始化 zoxide
if command -v zoxide > /dev/null
    zoxide init fish --cmd cd | source
end

# 手动设置 z 别名
if command -v zoxide > /dev/null
    function z
        __zoxide_z $argv
    end
    
    function zi
        __zoxide_zi $argv
    end
end

## System information
## Run fastfetch if session is interactive
#if status --is-interactive && type -q neofetch
#   neofetch
#end

function y
	set tmp (mktemp -t "yazi-cwd.XXXXXX")
	yazi $argv --cwd-file="$tmp"
	if read -z cwd < "$tmp"; and [ -n "$cwd" ]; and [ "$cwd" != "$PWD" ]
		builtin cd -- "$cwd"
	end
	rm -f -- "$tmp"
end
